<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController {

    /**
     * @var string the default layout for the controller view. Defaults to '//layouts/column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    public $layout = '//layouts/main';

    /**
     * @var array context menu items. This property will be assigned to {@link CMenu::items}.
     */
    public $menu = array();

    /**
     * @var array the breadcrumbs of the current page. The value of this property will
     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
     * for more details on how to specify this property.
     */
    public $breadcrumbs = array();
    public $Counter = array();
    public $viewbag;

    public function init() {
        //$this->countInweb();
    }

    public function countInweb() {
        $counter = Counter::model()->find(array(
            'select' => 'DATE',
            'order' => 'DATE DESC',
            'limit' => 1,
        ));

        if (isset($counter)) {
            if ($counter->attributes['DATE'] != Counter::model()->DateTimeServer->getNowDate()) {
                $daily = new Daily;
                $daily->DATE = $counter->attributes['DATE'];
                $daily->NUM = Counter::model()->count('DATE=:DATE', array(':DATE' => $counter->attributes['DATE']));
                $daily->save();

                Counter::model()->deleteAll('DATE=:DATE', array(':DATE' => $counter->attributes['DATE']));
            }
        }
        if (!Counter::model()->exists('IP=:IP', array(':IP' => $_SERVER['REMOTE_ADDR']))) {
            $counter = new Counter;
            $counter->DATE = Counter::model()->DateTimeServer->getNowDate();
            $counter->IP = $_SERVER['REMOTE_ADDR'];
            $counter->save();
        }

        $this->viewbag = array(
            'Today' => Counter::model()->count('DATE=:DATE', array(':DATE' => Counter::model()->DateTimeServer->getNowDate())),
            'Yesterday' => Daily::model()->countYesterday(),
            'This_Month' => Daily::model()->countThisMonth(),
            'Last_Month' => Daily::model()->countLastMonth(),
            'This_Year' => Daily::model()->countThisYear(),
            'Last_Year' => Daily::model()->countLastYear()
        );
    }

}

